<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
 */

require('includes/application_top.php');

require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_PASSWORD_FORGOTTEN);

if (isset($HTTP_GET_VARS['action']) && ($HTTP_GET_VARS['action'] == 'process') && isset($HTTP_POST_VARS['formid']) && ($HTTP_POST_VARS['formid'] == $sessiontoken)) {
    $email_address = tep_db_prepare_input($HTTP_POST_VARS['email_address']);

    $check_customer_query = tep_db_query("select customers_firstname, customers_lastname, customers_password, customers_id from " . TABLE_CUSTOMERS . " where customers_email_address = '" . tep_db_input($email_address) . "'");
    if (tep_db_num_rows($check_customer_query)) {
        $check_customer = tep_db_fetch_array($check_customer_query);

        $new_password = tep_create_random_value(ENTRY_PASSWORD_MIN_LENGTH);
        $crypted_password = tep_encrypt_password($new_password);

        tep_db_query("update " . TABLE_CUSTOMERS . " set customers_password = '" . tep_db_input($crypted_password) . "' where customers_id = '" . (int) $check_customer['customers_id'] . "'");

        tep_mail($check_customer['customers_firstname'] . ' ' . $check_customer['customers_lastname'], $email_address, EMAIL_PASSWORD_REMINDER_SUBJECT, sprintf(EMAIL_PASSWORD_REMINDER_BODY, $new_password), STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);

        $messageStack->add_session('login', SUCCESS_PASSWORD_SENT, 'success');

        tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
    } else {
        $messageStack->add('password_forgotten', TEXT_NO_EMAIL_ADDRESS_FOUND);
    }
}

$breadcrumb->add(NAVBAR_TITLE_1, tep_href_link(FILENAME_LOGIN, '', 'SSL'));
$breadcrumb->add(NAVBAR_TITLE_2, tep_href_link(FILENAME_PASSWORD_FORGOTTEN, '', 'SSL'));

require(DIR_WS_INCLUDES . 'template_top.php');

include('includes/form_check.js.php');
?>
<!--middle area start-->

<div class="middle-area">
  <div class="innermidbox fl">
    <div class="innermidbox-top"></div>
    <div class="innermidbox-mid">
      <div class="pdt5 pdl20 pdr20 pdb5 minheight527px">
        <div class="breadcrumbs"><a href="index.php">Home</a> > <span><?php echo HEADING_TITLE; ?></span></div>
        <div class="spacer10"></div>
        <div class="">
          <h1 class="pagetitle"><?php echo HEADING_TITLE; ?></h1>
          <div class="hline"></div>
          <div class="spacer3"></div>
          <!--left panel start-->
          <?php include('includes/template_left.php'); ?>
          <!--left panel end-->
          <!--right panel start-->
          <div class="rightpanel mgl15 fl">
            <?php
				if ($messageStack->size('password_forgotten') > 0) {
					echo $messageStack->output('password_forgotten');
				}
            ?>
            <?php echo tep_draw_form('password_forgotten', tep_href_link(FILENAME_PASSWORD_FORGOTTEN, 'action=process', 'SSL'), 'post', onsubmit="return check_forgot_password_form();", true); ?>
            <div class="contentContainer">
              <div class="contentText">
                <h3 class="newstitle tac"><?php echo TEXT_MAIN; ?></h3>
                <div class="spacer20"></div>
                <div class="contact-left mgt15"><?php echo ENTRY_EMAIL_ADDRESS; ?></div>
                <div class="hinputbg fl">
					<?php echo tep_draw_input_field('email_address', '', 'class="hinput"') ?>
                </div>
                <div class="spacer10"></div>
                <div class="contact-left">&nbsp;</div>
                <div class="fl mgl15">
                	<?php echo tep_submit('','class="continue-btn"'); ?>
                	<?php echo tep_draw_button(IMAGE_BUTTON_BACK, 'triangle-1-w', tep_href_link(FILENAME_LOGIN, '', 'SSL')); ?>
                </div>
                <div class="spacer5"></div>
              </div>
            </div>
            </form>
            <div class="spacer10"></div>
            <!--listing end-->
          </div>
          <!--right panel end-->
          <div class="spacer10"></div>
        </div>
      </div>
    </div>
    <div class="innermidbox-bottom"></div>
  </div>
</div>
<!--middle area end-->
<div class="spacer25"></div>
</div>
<?php
require(DIR_WS_INCLUDES . 'template_bottom.php');
require(DIR_WS_INCLUDES . 'application_bottom.php');
?>